package org.rui.rpc.serializer;

/**
 * @author: linweirui
 * @date: 2024/12/03 16:18
 */

import org.rui.rpc.constant.SerializerKeys;

import java.util.HashMap;
import java.util.Map;

/**
 * 序列化器工厂
 */
public class SerializerFactory {
    /**
     * 注册自定义序列器到map，便于用户自我选择
     */
    private static final Map<String, Serializer> SERIALIZER_MAP = new HashMap<String, Serializer>() {
        {
            put(SerializerKeys.JDK, new JdkSerializer());
            put(SerializerKeys.JSON, new JsonSerializer());
            put(SerializerKeys.HESSIAN, new HessianSerializer());
            put(SerializerKeys.KRYO, new KryoSerializer());
        }
    };
    /**
     * 设置默认序列化器 jdk
     */
    private static final Serializer DEFAULT_SERIALIZER = SERIALIZER_MAP.get(SerializerKeys.JDK);

    /**
     * 根据序列化key获取对应的序列化器
     *
     * @param key
     * @return
     */
    public static Serializer getSerializer(String key) {
        return SERIALIZER_MAP.getOrDefault(key, DEFAULT_SERIALIZER);
    }
}
